(function($,win){
    function previewImage(myImage){
        let maxWidth = $(window).width() - 300;
        let maxHeight = $(window).height() - 200;

        let imgNaturalWidth = myImage.naturalWidth;
        let imgNaturalHeight = myImage.naturalHeight;

        let layerWith, layerHeight;
        if (imgNaturalWidth < maxWidth && imgNaturalHeight < maxHeight) {
            layerWith = imgNaturalWidth;
            layerHeight = imgNaturalHeight;
        } else {
            layerWith = maxWidth;
            layerHeight = maxHeight;
        }
       
        var imgHtml = $(myImage.outerHTML).addClass('ui-answer-view-img')[0].outerHTML;

        layer.open({
            type: 1,
            title: false,
            closeBtn: 1,
            area: [layerWith + 'px', layerHeight + 'px'],
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: true,
            content: imgHtml
        });
        // 给图片增加事件
        var layerContent = $('.layui-layer-content');
        var div_img = $('img',layerContent);
        //绑定鼠标事件
        div_img.on("mousedown", function(event) {
            const imageGrabbing = 'ui-answer-view-img-grabbing';
            event.preventDefault && event.preventDefault();
            var t = $(this);
            var offset_x = t[0].offsetLeft; //x
            var offset_y = t[0].offsetTop; //y

            var mouse_x = event.pageX;
            var mouse_y = event.pageY;

            t.addClass(imageGrabbing);
            //
            layerContent.on("mousemove", function(ev) {
                var _x = ev.pageX - mouse_x;
                var _y = ev.pageY - mouse_y;
                //
                var now_x = (offset_x + _x) + "px";
                var now_y = (offset_y + _y) + "px";
                //
                div_img.css({
                    top: now_y,
                    left: now_x
                });
            });
            // 解绑mouseup事件
            layerContent.on("mouseup", function() {
                div_img.removeClass(imageGrabbing);
                layerContent.off("mousemove");
            });
        });
    }
	win._previewImage = previewImage;

})(jQuery,window);